package hello.app;

import hello.app.dialog.CommentBuilder;
import hello.app.dialog.ProgressDialogEx;
import hello.app.model.HelloApi;
import hello.app.model.ModelManager;
import hello.app.model.data.Comment;
import hello.app.model.data.HelloContact;
import hello.app.util.AdteAsyncTask;
import hello.app.util.Debug;
import hello.app.util.AdteAsyncTask.BackgroundRunner;
import hello.app.util.AdteAsyncTask.FinishRunner;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class CommentActivity extends Activity implements CommentBuilder.onSavedCommentListener{
	private ModelManager mManager;
	HelloContact mContact;

	public static void start(HelloContact contact, Context context){
		Intent in = new Intent(context,CommentActivity.class);
		in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		in.putExtra("contact", contact);
		context.startActivity(in);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mManager = ModelManager.getInstatce(this);
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			mContact = (HelloContact) extras.getSerializable("contact");
			if(!showCommentDlg(mContact.getId())){
				finish();
			}
		}else{
			finish();
		}
	}

	public void onSaved(String text) {
		ProgressDialogEx dlg = new ProgressDialogEx(this,
				"口コミを保存中です。しばらくお待ちください");
		AdteAsyncTask task = new AdteAsyncTask(new BackgroundRunner() {
			public Object exec(Object... args) {
				String text = (String) args[0];
				HelloApi api = mManager.getHelloApi();
				boolean ret = false;
				try {
					Comment cmt = api.saveComment(mContact.getId(), text);
					mManager.getCommentModel().save(cmt);
					ret = true;
				} catch (Exception e) {
					Debug.error(e.getMessage());
					ret = false;
				}
				return ret;
			}
		}, new FinishRunner() {
			public void onFinish(Object success) {
				if ((Boolean)success) {
					Toast.makeText(CommentActivity.this, "口コミを保存しました",
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(CommentActivity.this,
							"口コミを保存ができませんでした。ネットワークに繋がっているか確認してください",
							Toast.LENGTH_LONG).show();
				}
				finish();
			}
		}, (ProgressDialog) dlg);
		// 非同期(スレッド)処理の実行
		task.execute(text);
	}

	private boolean showCommentDlg(int contact_id){
		Comment cmt = mManager.getCommentModel().findUnique(contact_id, mManager.getHelloSetting().getUserId());
    	String org = cmt != null ? cmt.getText() : "";
    	if(org.length() != 0)
    		return false;
    	CommentBuilder builder = new CommentBuilder(this, org);
    	builder.setOnSavedCommentListener(this);
    	builder.setNegativeButton("キャンセル", new OnClickListener() {
			public void onClick(DialogInterface dialoginterface, int i) {
				dialoginterface.cancel();
				CommentActivity.this.finish();
			}
		});
    	builder.create().show();
    	return true;
	}
}
